package com.example.imoocmall.service.impl;

import com.example.imoocmall.exception.ImoocMallException;
import com.example.imoocmall.exception.ImoocMallExceptionEnum;
import com.example.imoocmall.model.dao.CategoryMapper;
import com.example.imoocmall.model.pojo.Category;
import com.example.imoocmall.model.request.AddCategoryReq;
import com.example.imoocmall.service.CategoryService;
import com.example.imoocmall.model.vo.CategoryVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 分类目录实现
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public void add(AddCategoryReq addCategoryReq){
        Category category = new Category();
        BeanUtils.copyProperties(addCategoryReq,category);//复制进去
        Category categoryOld = categoryMapper.selectByName(addCategoryReq.getName());//判断类名充不重复
        if (categoryOld != null){
            throw new ImoocMallException(ImoocMallExceptionEnum.NAME_EXISTED);
        }
        int count = categoryMapper.insertSelective(category);
        if (count!=1){
            throw new ImoocMallException(ImoocMallExceptionEnum.CREATE_FAILED);
        }
    }
    @Override
    public void update(Category updateCategory){
        Category categoryOld = categoryMapper.selectByName(updateCategory.getName());
        if (categoryOld != null && !categoryOld.getId().equals(updateCategory.getId())){//新的类名不存在.并且不是他自己
            throw new ImoocMallException(ImoocMallExceptionEnum.NAME_EXISTED);
        }
        int count = categoryMapper.updateByPrimaryKeySelective(updateCategory);
        if (count != 1){
            throw new ImoocMallException(ImoocMallExceptionEnum.UPDATE_FAILED);
        }

    }

    @Override
    public void delete(Integer id){
        Category category = categoryMapper.selectByPrimaryKey(id);
        if (category == null){
            throw new ImoocMallException(ImoocMallExceptionEnum.DELETE_FAILED);
        }
        int count = categoryMapper.deleteByPrimaryKey(id);
        if (count == 0){
            throw new ImoocMallException(ImoocMallExceptionEnum.DELETE_FAILED);
        }
    }

    @Override
    public PageInfo listForAdmin(Integer pageNum, Integer pageSize){

        PageHelper.startPage(pageNum,pageSize);
        List<Category> categoryList = categoryMapper.selectList();
        PageInfo pageInfo = new PageInfo(categoryList);
        return pageInfo;
    }

//    @Override
//    public List<CategoryVo> listCategoryForCustomer(){
//        List<CategoryVo> categoryVOList = new ArrayList<>();
//        recursivelyFindCategories(categoryVOList,0);
//        return categoryVOList;
//    }
//    private void recursivelyFindCategories(List<CategoryVo> categoryVOList,Integer parentId){
//        List<Category> categoryList = categoryMapper.selectCategoriesByParentId(parentId);
//        if (!CollectionUtils.isEmpty(categoryList)){
//            for (int i = 0; i < categoryList.size(); i++) {
//                Category category =  categoryList.get(i);
//                CategoryVo categoryVo = new CategoryVo();
//                BeanUtils.copyProperties(category,categoryVo);
//                categoryVOList.add(categoryVo);
//                recursivelyFindCategories(categoryVo.getChildCategory(),categoryVo.getId());
//            }
//        }
//    }

    @Override
    @Cacheable(value = "listCategoryForCustomer")
    //传进去的是parentId，但是会传出父类id是这个的类别为第一个类别的递归
    public List<CategoryVo> listCategoryForCustomer(Integer parentId){
        List<CategoryVo> categoryVoList = new ArrayList<>();
        recursivelyFindCategories(categoryVoList,parentId);
        return categoryVoList;
    }
    public void recursivelyFindCategories(List<CategoryVo> categoryVoList,Integer parentId){
        List<Category> categoryList = categoryMapper.selectCategoriesByParentId(parentId);
        if (categoryList!=null){
            for (Category category : categoryList) {
                CategoryVo categoryVo = new CategoryVo();
                BeanUtils.copyProperties(category, categoryVo);
                categoryVoList.add(categoryVo);
                recursivelyFindCategories(categoryVo.getChildCategory(), categoryVo.getId());
            }
        }
    }
}

















